<template>
  <div class="app-container">
    <el-row :gutter="40" class="panel-group">
      <el-col :xs="24" :sm="24" :lg="12">
        <el-col :xs="24" :sm="24" :lg="12" class="card-panel-col">
          <el-card shadow="always">
            <div class="card-panel">
              <div class="card-panel-description">
                <div class="card-panel-text">账户余额（dlb）</div>
                <div class="card-panel-num">
                  <count-to :start-val="0" :end-val="balance" :duration="2000" />
                  <i :class="balanceLoading ? 'el-icon-loading' : 'el-icon-refresh'" class="icon" @click="refresh"/>
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :xs="24" :sm="24" :lg="12" class="card-panel-col">
          <div class="card-panel-btn">
            <el-button class="filter-item" type="default" @click="$router.push('/account/recharge')">充值</el-button>
            <el-button class="filter-item" type="primary" @click="$router.push('/account/cash')">提现</el-button>
          </div>
        </el-col>
      </el-col>
    </el-row>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="时间" align="center" width="200px">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" prop="type" align="center" width="150px">
        <template slot-scope="scope">
          <el-tag :type="(scope.row.type==1) ?'success':(scope.row.type==2?'danger':'')">{{ scope.row.type | filterType }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="转账地址" align="center">
        <template slot-scope="scope">
          <span class="link-type">{{ scope.row.sender }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收款地址" align="center">
        <template slot-scope="scope">
          <span class="link-type">{{ scope.row.recipient }}</span>
        </template>
      </el-table-column>
      <el-table-column label="交易费(dlb)" align="center" width="150px">
        <template slot-scope="scope">
          <span>{{ '1' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额(dlb)" align="center" width="150px">
        <template slot-scope="scope">
          <span>{{ scope.row.amount }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="fetchData" />
  </div>
</template>

<script>
import CountTo from 'vue-count-to'
import Pagination from '@/components/Pagination'
import { getBalance, getRechargeRecord } from '@/api/table'
import { setToken } from '@/utils/auth'
export default {
  name: 'ComplexTable',
  components: { CountTo, Pagination },
  filters: {
    filterType(type) {
      switch (type) {
        case 1:
          return '充值'
        case 2:
          return '提现'
        case 3:
          return 'DLB官方'
      }
    }
  },
  data() {
    return {
      balance: 0,
      balanceLoading: false,
      list: null,
      total: 0,
      listLoading: false,
      listQuery: {
        currentPage: 1,
        pageSize: 15
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.fetchBalance()
      this.fetchRechargeRecord()
    },
    fetchBalance() {
      this.balanceLoading = true
      getBalance({}).then(response => {
        setTimeout(() => {
          this.balanceLoading = false
          this.balance = response.data.balance
          setToken('balance', response.data.balance)
        }, 500)
      })
    },
    fetchRechargeRecord() {
      this.listLoading = true
      getRechargeRecord(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      })
    },
    refresh() {
      this.fetchBalance()
    }
  }
}
</script>

<style lang="scss" scoped>
.panel-group{
	margin-bottom: 20px;
	.card-panel {
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    .card-panel-description {
      font-weight: bold;
      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }
      .card-panel-num {
        width:100%;
				font-size: 26px;
				display: inline-block;
				text-align: right;
        .icon{
          font-size:20px;
        }
      }
		}
	}
	.card-panel-btn{
		height: 100px;
		.el-button{
			margin-top: 35px;
		}
	}
}
</style>
